/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package xenon3d.scene;

import javax.media.opengl.GL2;

/**
 * The PointAttributes object defines all attributes that apply to point
 * primitives.
 * 
 * @author Volker Everts
 * @version 0.1 - 10.12.2009: Created
 * @version 0.2 - 12.11.2011: Adapted
 */
public class PointAttributes extends Attributes {
    
    // <editor-fold defaultstate="collapsed" desc=" Static Attributes ">
    
    /** The default point attributes. */
    static final PointAttributes DEFAULT = new PointAttributes();
    
    /** The last active PointAttributes. */
    static PointAttributes last = DEFAULT;
    
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc=" Private Fields ">
    
    /** The point size or line width. */
    private float size = 1.0f;
    
    /** The antialiasing flag. */
    private boolean antialiasing = false;
    
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc=" Initialization ">
    
    /**
     * Creates a new PointAttributes object using default parameter settings.
     * The default settings are:<p>
     * <ul>
     * <li>point size: 1.0f</li>
     * <li>antialiasing: false</li>
     * </ul>
     */
    public PointAttributes() {}
    
    /**
     * Creates a new PointAttributes object using the specified size and
     * antialiasing flag.
     * @param size the point size
     * @param antialiasing if true, point antialiasing is enabled
     */
    public PointAttributes(float size, boolean antialiasing) {
        this.size = size;
        this.antialiasing = antialiasing;
    }
    
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc=" Public Properties ">
    
    /**
     * Sets the point size to the specified value.
     * @param size the new point size
     */
    public final void setPointSize(float size) {
        checkCapability(Capability.AllowPointAttributesWrite);
        if (size < 0.0f) throw new IllegalArgumentException();
        this.size = size;
    }
    
    /**
     * Returns the point size attribute.
     * @return the current point size
     */
    public final float getPointSize() {
        return size;
    }
    
    /**
     * Sets whether or not point antialiasing should be enabled.
     * @param enable if true, point antialiasing will be enabled
     */
    public final void setPointAntialiasing(boolean enable) {
        checkCapability(Capability.AllowPointAttributesWrite);
        antialiasing = enable;
    }
    
    /**
     * Returns the current antialiasing state.
     * @return true, if point antialiasing is enabled
     */
    public final boolean isPointAntialiasing() {
        return antialiasing;
    }
    
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc=" Package Private Methods (OpenGL) ">
    
    @Override
    void apply(GL2 gl) {
        if (this == last) return;
        if (size != last.size) gl.glPointSize(size);
        if (antialiasing != last.antialiasing) {
            if (antialiasing) gl.glEnable(GL2.GL_POINT_SMOOTH);
            else gl.glDisable(GL2.GL_POINT_SMOOTH);
        }
        last = this;
    }
        
    // </editor-fold>

} // end class PointAttributes